import java.util.*;


class TreeNode {
   int val = 0;
   TreeNode left = null;
   TreeNode right = null;
   public TreeNode(int val) {
     this.val = val;
   }
 }


public class BinaryTreeTest {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param root TreeNode类 
     * @return int整型一维数组
     */
    public int[] preorderTraversal (TreeNode root) {
        //1.创建一个存储数组
        List<Integer> list = new ArrayList<>();
        //2.遍历
         preorderDfs(list,root);
        //3.取出链表元素
        int[] ans = new int[list.size()];
        for(int i = 0;i<list.size();i++){
            ans[i] = list.get(i);
        }
        return ans;

    }
    private void  preorderDfs(List<Integer> list , TreeNode root){
        if(root == null) return;
        //添加当前节点
        list.add(root.val);
        //先去左子树
         preorderDfs(list,root.left);
        //再去右子树
         preorderDfs(list,root.right);
    }
    public int[] inorderTraversal (TreeNode root) {
        // write code here
        List<Integer> list = new ArrayList<>();
        inorderDfs(list,root);
        int[] ans = new int[list.size()];
        for(int i = 0;i<list.size();i++){
            ans[i] = list.get(i);
        }
        return ans;
    }
    private void inorderDfs(List<Integer> list , TreeNode root){
        if(root == null) return;
        //先去左子树
         preorderDfs(list,root.left);
        //添加当前节点
        list.add(root.val);
        //再去右子树
         preorderDfs(list,root.right);
    }
}
}